//
//  HomeTableViewController.swift
//  SinaWebo
//
//  Created by mis on 16/3/17.
//  Copyright © 2016年 mis. All rights reserved.
//

import UIKit
import AFNetworking
import SVProgressHUD

private let HomeCellId = "HomeCellId"
class HomeTableViewController: BaseTableViewController {
    
    private lazy var statuses = [Status]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        SVProgressHUD.show()
        visitorLoginView?.setupOtherUI(nil, title: "关注一些人，回这里看看有什么惊喜")
        navigationController?.hidesBottomBarWhenPushed = true
        prepareTableView()
        loadData()
    }
    

    
    private func prepareTableView() {
        refreshControl = UIRefreshControl()
        refreshControl?.addTarget(self, action: "loadData", forControlEvents: .ValueChanged)
        tableView.registerClass(StatusCell.self, forCellReuseIdentifier: HomeCellId)
        tableView.estimatedRowHeight = 200
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.separatorStyle = .None
        tableView.tableFooterView = indicatorView
    }
    
    @objc private func loadData() {
        var since_id = 0
        var max_id = 0
        if indicatorView.isAnimating() {
            //上拉加载更多数据
            since_id = 0
            max_id = statuses.last?.id ?? 0
        } else {
            //下拉刷新
            max_id = 0
            since_id = statuses.first?.id ?? 0
        }
        StatusListViewModel.loadData(since_id, max_id: max_id) { (array, error) -> () in
            self.refreshControl?.endRefreshing()
            if array == nil && error != nil {
                SVProgressHUD.showErrorWithStatus(appErroTip)
                self.indicatorView.stopAnimating()
                return
            }
            if array == nil && error == nil {
                SVProgressHUD.showInfoWithStatus("数据已经是最新")
                self.indicatorView.stopAnimating()
                return
            }
            
            if since_id > 0 {
                // 下拉刷新
                self.statuses = array! + self.statuses
            }else if max_id > 0 {
                self.statuses += array!
                self.indicatorView.stopAnimating()
            }else {
                self.statuses = array!
            }
            SVProgressHUD.dismiss()
            self.tableView.reloadData()
        }
    }
    
    private lazy var indicatorView: UIActivityIndicatorView = {
        let indicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
        return indicator
    }()
    
}

extension HomeTableViewController {
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.statuses.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(HomeCellId, forIndexPath: indexPath) as! StatusCell
        let status = statuses[indexPath.row]
        cell.status = status
        if indexPath.row == statuses.count - 1 && !indicatorView.isAnimating() {
            indicatorView.startAnimating()
            loadData()
            print("开始静默加载数据")
        }
        return cell
    }
}

